<?php 
namespace app\admin\controller;

use app\common\model\shop\ShopClassify;
use app\common\controller\Base64;
use think\Config;
use think\Exception;
use app\common\controller\Log;
use think\Db;
use app\common\controller\Classify as ClassifyContror;

class Classify extends Base
{
	/**
	 * 获取分类列表
	 * @return unknown
	 */
	public function typeList()
	{
		$classify = new ClassifyContror();
		$list = $classify->getClassifyList();
		return jsonSuccess($list);
	}
	/**
	 * 添加分类
	 * @throws Exception
	 * @return unknown
	 */
	public function addType()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			unset($data['parent_name']);
			if(!$data['name'])throw new Exception('请输入分类名称');
			if(!$data['icon'])throw new Exception('请现在分类图标');
			$data['create_time'] = time();
			$data['update_time'] = time();
			ShopClassify::create($data);
			//添加日志
			Log::addLog(1, '添加分类【'.$data['name'].'】');
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('添加成功');
	}
	
	/**
	 * 获取分类信息
	 * @return unknown
	 */
	public function getTypeInfo()
	{
		$id = $this->request->post('id');
		$classify = Db::name('shop_classify')->where(['id' => $id])->find();
		return jsonSuccess($classify);
	}
	
	/**
	 * 修改分类
	 * @throws Exception
	 * @return unknown
	 */
	public function editType()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			unset($data['parent_name']);
			if(!$data['name'])throw new Exception('请输入分类名称');
			if(!$data['icon'])throw new Exception('请现在分类图标');
			$classify = ShopClassify::get($data['id']);
			$data['update_time'] = time();
			$classify->save($data);
			//添加日志
			Log::addLog(2, "修改分类【{$classify->name}】");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('修改成功');
	}
	
	/**
	 * 删除分类
	 * @throws Exception
	 * @return unknown
	 */
	public function deleteType()
	{
		$id = $this->request->post('id');
		Db::startTrans();
		try {
			$classify = ShopClassify::get($id);
			if(!$classify->state)throw new Exception('状态错误');
			$classify->save(['update_time' => time(),'state' => 0]);
			//删除所有的下级分类
			$parent_id = ShopClassify::where(['parent_id' => $id , 'state' => 1])->column('id');
			$parent_id[] = $id;
			ShopClassify::where(['parent_id' => ['in',$parent_id] , 'state' => 1])->update(['update_time' => time(),'state' => 0]);
			//添加日志
			Log::addLog(3, "删除分类【{$classify->name}】");
			Db::commit();
		}catch (Exception $e){
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('删除成功');
	}
	/**
	 * 获取父级菜单
	 * @return unknown
	 */
	public function getParentName()
	{
		$parent_id = $this->request->post('parent_id');
		if(!$parent_id){
			return jsonSuccess(['name' => '顶级菜单']);
		}else{
			$classify = ShopClassify::get($parent_id);
			return jsonSuccess($classify);
		}
	}
	/**
	 * 上传图片
	 * @return unknown
	 */
	public function upImage()
	{
		$data = $this->request->post('');
		$base64 = new Base64();
		$res = $base64 ->up_base64($data['img'], Config::get('path.classify'));
		return jsonSuccess($res);
	}
}